

import pandas as pd
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import numpy as np

# 读取CSV文件
df = pd.read_csv('./data/数据分析/nba2k-full.csv')

# 1. 分析在哪个版本的球员中，平均评分最高
version_avg_rating = df.groupby('version')['rating'].mean().reset_index()
version_avg_rating = version_avg_rating.sort_values(by='rating', ascending=False)
highest_rated_version = version_avg_rating.iloc[0]['version']
highest_rated_version_avg = version_avg_rating.iloc[0]['rating']

# 绘制条形图展示平均评分最高的球员版本
plt.figure(figsize=(10, 6))
plt.bar(version_avg_rating['version'], version_avg_rating['rating'])
plt.title('Average Rating by Version')
plt.xlabel('Version')
plt.ylabel('Average Rating')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

# 2. 分析哪些大学为NBA输送了最多的球员
college_counts = df['college'].value_counts().reset_index()
college_counts.columns = ['college', 'player_count']
most_players_college = college_counts.iloc[0]['college']
most_players_count = college_counts.iloc[0]['player_count']

# 绘制条形图展示为NBA输送了最多球员的大学
plt.figure(figsize=(10, 6))
plt.bar(college_counts['college'][:10], college_counts['player_count'][:10])
plt.title('Top 10 Colleges by Player Count')
plt.xlabel('College')
plt.ylabel('Player Count')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

# 绘制饼图展示各大学输送球员的比例
college_percentages = college_counts['player_count'] / college_counts['player_count'].sum() * 100
plt.figure(figsize=(10, 6))
plt.pie(college_percentages[:10], labels=college_counts['college'][:10], autopct='%1.1f%%', startangle=140)
plt.title('Percentage of Players from Top 10 Colleges')
plt.show()

# 绘制词云图展示各大学输送球员的数量
wordcloud = WordCloud(width=800, height=400, background_color='white', min_font_size=10).generate_from_frequencies(college_counts.set_index('college')['player_count'].to_dict())
plt.figure(figsize=(10, 6))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()

print(f"平均评分最高的版本是：{highest_rated_version}，平均评分为：{highest_rated_version_avg}")
print(f"为NBA输送了最多球员的大学是：{most_players_college}，输送了{most_players_count}名球员")